home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7145 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C constant expression declarations
  5. Date: Sat, 17 Feb 96 22:23:12 GMT
  6. Organization: none
  7. Message-ID: <824595792snz@genesis.demon.co.uk>
  8. References: <31229735.41C67EA6@isi.com> <4fvl5cINN94q@keats.ugrad.cs.ubc.ca> <4g2nha$ksa@sun001.spd.dsccc.com> <4g3fkeINNoj7@keats.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4g3fkeINNoj7@keats.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >Parentheses are intended to override operator precedence, and to occasionally
  18. >make a complex expression clearer to someone reading them; a proverbial icing
  19. >on the cake of sorts.  
  20. >
  21. >if (x == 3 || y > 4 && z < 5)  
  22. >
  23. >is easier to read than
  24. >
  25. >if ((x == 3) || ((y > 4) && (z < 5))) 
  26.  
  27. However these examples have little relevance to the case in question which is
  28. macro definitions of expressions. Here enclosing parentheses are essential
  29. to ensure correct grouping after substitution within a more general
  30. expression. The only exception is when the replacement text consists of
  31. a single token e.g.
  32.  
  33. #define VAL1    1
  34.  
  35. Now the question is whether this should be treated as a special case and
  36. not written with parentheses, or written with parentheses to maintain a
  37. consistent style and reduce the chance of mistakes such as:
  38.  
  39. #define VALX    -1
  40.  
  41. Expression complexity is not an issue here since the case where this is an
  42. issue is by its very nature the simplest possible one. Since negative
  43. constants must be written with parentheses it is natural for positive
  44. constants to be written in a consistent way.
  45.  
  46. #define RESULT_A    (0)
  47. #define RESULT_B    (-1)
  48.  
  49. is easier or at least more consistent and more pleasant to read than:
  50.  
  51. #define RESULT_A    0
  52. #define RESULT_B    (-1)
  53.  
  54. (generalise to larger lists of codes).
  55.  
  56. >I put my extra discipline into consciously knowing what I'm doing. I _am_
  57. >awake; I _did_ drink my coffee. I will not write things that don't _buy_
  58. >anything. They obfuscate programs more than they help.
  59.  
  60. Anybody can make mistakes. Using a consistent style means they are much
  61. easier to spot. Something like this is likely to be a project-wide
  62. convention. Do you trust other people as much as you trust yourself?
  63.  
  64. -- 
  65. -----------------------------------------
  66. Lawrence Kirby | fred@genesis.demon.co.uk
  67. Wilts, England | 70734.126@compuserve.com
  68. -----------------------------------------
  69.